//
//  ShoppingCartTableView.m
//  wtshoppingcite
//
//  Created by TW on 14-12-1.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "ShoppingCartTableView.h"
#import "ShoppingCartCell.h"
#import "ShoppingCartModel.h"

@implementation ShoppingCartTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if(self){
        self.backgroundView = nil;
        self.backgroundColor = [UIColor clearColor];
        self.dataSource = self;
        self.delegate = self;
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        _flag = NO;
    }
    return self;
}


#pragma mark tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.data.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShoppingCartCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cellIdentitfer"];
    if (cell == nil) {
        cell = [[ShoppingCartCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentitfer"];
        cell.selectionStyle = UITableViewCellAccessoryNone;
        
        UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(5, 40, 20, 20)];
        button.tag = indexPath.row + 1000;
        button.hidden = YES;
        [button addTarget:self action:@selector(selectedAddressAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:button];
        
    }
    
    UIButton* button = (UIButton*)[cell.contentView viewWithTag:indexPath.row + 1000];
   [button setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
    
    if(_isEdit == YES){
        button.hidden = NO;
    }else{
        button.hidden = YES;
    }
    if (_allSelect == YES) {
        
        [button setImage:[UIImage imageNamed:@"choose_select_org.png"] forState:UIControlStateNormal];
         [[RegisterHelper shareHelper].deleteArray addObjectsFromArray:self.data];
        
    }else if(_allSelect == NO){
        [button setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
        [[RegisterHelper shareHelper].deleteArray removeAllObjects];
    }

    cell.isEidt = self.isEdit;
    cell.shoppingModel =self.data[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

#pragma mark 选中
-(void)selectedAddressAction:(UIButton *)btn{

        long index = btn.tag - 1000;
    
        if(_flag == NO){
            [btn setImage:[UIImage imageNamed:@"choose_select_org.png"] forState:UIControlStateNormal];
            _flag = YES;
            
            [[RegisterHelper shareHelper].deleteArray addObject:self.data[index]];
            
        }else{
            
            [btn setImage:[UIImage imageNamed:@"choose"] forState:UIControlStateNormal];
            _flag = NO;
            [[RegisterHelper shareHelper].deleteArray removeObject:self.data[index]];
        }
    
        NSLog(@"需删除的数组下标%@",[RegisterHelper shareHelper].deleteArray);
   
}


@end
